package com.bing.my.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bing.common.config.Result;
import com.bing.my.entity.Goose;
import com.bing.my.service.GooseService;
import com.bing.utils.MyFileUtil;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

/**
 * <p>
 * 商品数据 前端控制器
 * </p>
 *
 * @author bing
 * @since 2021-10-04
 */

@RestController
@RequestMapping("/goose")
public class GooseController {
    @Resource
    private GooseService gooseService;

    @Autowired
    MyFileUtil myFileUtil;


    /**
     * 新增产品
     * 需要登录权限
     *
     * @param goose
     * @return com.bing.common.config.Result<?>
     * @author Bing
     * @date 2021/10/09 17:50
     */
    @RequiresAuthentication
    @PostMapping
    public Result<?> insert(@Validated @RequestBody Goose goose) {
        if (gooseService.insert(goose) != 1) {
            return Result.error();
        }
        return Result.success();
    }

    /**
     * 商品分页查询
     * 需要登录权限
     *
     * @param pageNum  页码
     * @param pageSize 显示页数
     * @param search   搜索关键字
     * @param select   搜索类型
     * @return com.bing.common.config.Result<?>
     * @author Bing
     * @date 2021/10/09 20:45
     */
    @RequiresAuthentication
    @GetMapping
    public Result<?> findPage(@RequestParam Integer pageNum,
                              @RequestParam Integer pageSize,
                              @RequestParam String search,
                              @RequestParam String select) {
        Page<Goose> goosePage = gooseService.selectPage(search, select, pageNum, pageSize);
        return Result.success(goosePage);
    }

    /**
     * 商品更新方法
     * 需要登录权限
     *
     * @param goose
     * @return com.bing.common.config.Result<?>
     * @author Bing
     * @date 2021/10/09 21:53
     */
    @RequiresAuthentication
    @PutMapping
    public Result<?> update(@Validated @RequestBody Goose goose) {
        int update = gooseService.update(goose);
        if (update != 1) {
            return Result.error(501, "商品修改失败!");
        }
        return Result.success("修改成功!");
    }

    /**
     * 删除商品
     * 需要登录权限
     *
     * @param id
     * @return com.bing.common.config.Result<?>
     * @author Bing
     * @date 2021/10/10 15:13
     */
    @RequiresAuthentication
    @DeleteMapping("/{id}")
    public Result<?> deleteById(@PathVariable String id) {
        int deleteById = gooseService.deleteById(id);
        if (deleteById != 1) {
            return Result.error(501, "商品删除失败!");
        }
        return Result.success("删除成功!");
    }

    /**
     * 上传图片
     * 需要登录权限
     * @param file
     * @return com.bing.common.config.Result<?>
     * @author Bing
     * @date 2021/10/10 15:18
     */
    @RequiresAuthentication
    @PostMapping("/upload")
    public Result<?> upload(MultipartFile file) {
        String url = myFileUtil.upload(file, "/goose","/files/gooseImg");
        if (url == null) {
            return Result.error(50000, "上传失败");
        }
        return Result.success(url);
    }

    /**
     * 下载图片
     * 需要登录权限
     *
     * @param flag
     * @param response
     * @return
     */
    @GetMapping("/files/gooseImg/{flag}")
    public Result<?> getFiles(@PathVariable String flag, HttpServletResponse response) {
        int msg = myFileUtil.getFiles(flag, "/files/gooseImg", response);
        if (msg == -1) {
            return Result.error(50000, "下载失败");
        }
        return Result.success();
    }
    /**
    * 删除图片
     * 需要登录权限
    * @author Bing
    * @date 2021/10/12 17:49
     * @param goose
    * @return com.bing.common.config.Result<?>
    */
    @RequiresAuthentication
    @PostMapping ("/deleteFiles")
    public Result<?> deleteFiles(@RequestBody Goose goose) {

        boolean deleteFiles = myFileUtil.deleteFiles(goose.getPhotoUrl(), "/files/gooseImg");
        if (!deleteFiles){
            return Result.error(50000, "删除失败");
        }
        return Result.success("取消成功!");
    }

}

